package com.coffee.admin.controller;

import com.coffee.core.anotation.OperLog;
import com.coffee.core.enums.ResourceBucketEnum;
import com.coffee.core.result.Result;
import com.coffee.admin.utils.ResourceUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/api")
@Controller
public class PictureController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(PictureController.class);

    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.bucket}")
    private String bucket;
    @Resource
    private ResourceUtils resourceUtils;


    @OperLog(operModul = "文件模块", operDesc = "文件上传")
    @ResponseBody
    @RequestMapping("/upload/resource")
    public Result<Map<String, String>> uploadImg(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {

            // 上传文件名
            MultipartFile mf = entity.getValue();
            String originalFilename = mf.getOriginalFilename();
            logger.info("===============originalFilename===============" + originalFilename);
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String key = ResourceUtils.initKey(suffix);
            logger.info("===============key===============" + key);
            try {
                String returnKey = resourceUtils.upload(bucket, key, mf.getBytes());
                if (StringUtils.isEmpty(returnKey)) {
                    return Result.failed(8888, "上传失败");
                } else {
                    String resourceId = UUID.randomUUID().toString();
                    String url = "";
                    endpoint = endpoint.replaceAll("-internal", "");
                    url = new StringBuilder("https://").append(bucket).append(".").append(endpoint).append("/").append(key).toString();
                    retMap.put("key", key);
                    retMap.put("url", url);
                    retMap.put("resourceId", resourceId);
                    return Result.success(retMap);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return Result.failed(8888, "上传失败");
    }
}
